package com.fenqing.leetcode.code.t0_200.t0_100;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

import java.util.HashSet;
import java.util.Set;

/**
 * @author fenqing
 */
public class Topic036 extends Topic {
    /**
     * 构造器
     *
     * @param input 输入
     */
    public Topic036(String input) {
        super(input);
    }

    @Execute
    public boolean isValidSudoku(char[][] board) {
        int i = board.length, j = board[0].length;
        Set<Character>[] lines = new HashSet[i];
        Set<Character>[] columns = new HashSet[i];
        Set<Character>[] blocks = new HashSet[i];
        for (int z = 0; z < i; z++) {
            lines[z] = new HashSet<>();
            columns[z] = new HashSet<>();
            blocks[z] = new HashSet<>();
        }
        for (int k = 0; k < i; k++) {
            char[] chars = board[k];
            Set<Character> line = lines[k];
            for (int l = 0; l < j; l++) {
                char ch = chars[l];
                Set<Character> column = columns[l];
                Set<Character> block = blocks[k / 3 * 3 + l / 3];
                if (ch != '.') {
                    if (line.contains(ch)) {
                        return false;
                    } else if (column.contains(ch)) {
                        return false;
                    } else if (block.contains(ch)) {
                        return false;
                    } else {
                        line.add(ch);
                        column.add(ch);
                        block.add(ch);
                    }
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Topic036 topic036 = new Topic036("""
                [
                ["5","3",".",".","7",".",".",".","."],
                ["6",".",".","1","9","5",".",".","."],
                [".","9","8",".",".",".",".","6","."],
                ["8",".",".",".","6",".",".",".","3"],
                ["4",".",".","8",".","3",".",".","1"],
                ["7",".",".",".","2",".",".",".","6"],
                [".","6",".",".",".",".","2","8","."],
                [".",".",".","4","1","9",".",".","5"],
                [".",".",".",".","8",".",".","7","9"]
                ]
                """);
        Boolean run = topic036.run();
        System.out.println(run);
    }
}
